package org.jsbd.boss.action;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseMapUtils;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.DouyingActionConfig;
import org.jsbd.boss.domian.DouyingActionStep;
import org.jsbd.boss.enums.DouyingStepTypeStatusEnum;
import org.jsbd.boss.service.IDouyingActionConfigService;
import org.jsbd.boss.service.IDouyingActionStepConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 抖音配置
 * @author lhz
 * @date:   2018年8月22日 上午9:06:03
 */
@Controller
@RequestMapping("/douying/action")
public class DouYingActionConfigController {
	public static final Logger LOGGER = LoggerFactory.getLogger(DouYingActionConfigController.class);

	@Autowired
	private IDouyingActionConfigService douyingActionConfigService;
	@Autowired
	private IDouyingActionStepConfigService douyingActionStepConfigService;
	
	@RequestMapping(value = "/config", method = RequestMethod.GET)
	public String actionConfig(Model model) {
		return "douying/actionConfig";
	}
	
	/**
	 * action页面
	 * @param page
	 * @param douyingActionConfig
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getListPage")
	@ResponseBody
	public Map<String, Object> getListPage(DataGridModel page,  DouyingActionConfig douyingActionConfig) throws Exception {
		return douyingActionConfigService.getListPage(page, douyingActionConfig);
	}
	

	/**
	 * action新增
	 * @param douyingActionConfig
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(DouyingActionConfig douyingActionConfig) {
		douyingActionConfigService.save(douyingActionConfig);
		return ResponseMapUtils.responseSuccess();
	}
	
	/**
	 * actionx修改
	 * @param douyingActionConfig
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Object update(DouyingActionConfig douyingActionConfig) {
		if (douyingActionConfig.getId() == null) {
			return ResponseMapUtils.responseFail("参数缺失，请确认参数再提交");
		}
		douyingActionConfigService.update(douyingActionConfig);
		return ResponseMapUtils.responseSuccess();
	}
	
	/**
	 * action删除
	 * @param ids
	 * @param response
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(Integer[] ids, HttpServletResponse response) {
		if (ids.length == 0) {
			ResponseUtils.responseFailure(response);
		}
		try {
			List<Integer> list = Arrays.asList(ids);
			douyingActionConfigService.delete(list);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			ResponseUtils.responseFailure(response);
		}
	}
	
	/**
	 * step页面
	 * @param page
	 * @param douyingActionConfig
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/step/getListPage")
	@ResponseBody
	public Map<String, Object> getStepListPage(DataGridModel page,  DouyingActionStep douyingActionStep) throws Exception {
		if (douyingActionStep.getActionId() == null) {
			return new HashMap<>();
		}
		return douyingActionStepConfigService.getListPage(page, douyingActionStep);
	}
	
	/**
	 * step新增
	 * @param douyingActionConfig
	 * @return
	 */
	@RequestMapping(value = "/step/save", method = RequestMethod.POST)
	@ResponseBody
	public Object stepSave(DouyingActionStep douyingActionStep) {
		douyingActionStep.setStepName(DouyingStepTypeStatusEnum.valueOf(douyingActionStep.getStepNum()).getName());
		douyingActionStepConfigService.save(douyingActionStep);
		return ResponseMapUtils.responseSuccess();
	}
	
	/**
	 * actionx修改
	 * @param douyingActionConfig
	 * @return
	 */
	@RequestMapping(value = "/step/update", method = RequestMethod.POST)
	@ResponseBody
	public Object stepUpdate(DouyingActionStep douyingActionStep) {
		if (douyingActionStep.getId() == null) {
			return ResponseMapUtils.responseFail("参数缺失，请确认参数再提交");
		}
		douyingActionStep.setStepName(DouyingStepTypeStatusEnum.valueOf(douyingActionStep.getStepNum()).getName());
		douyingActionStepConfigService.update(douyingActionStep);
		return ResponseMapUtils.responseSuccess();
	}
	
	/**
	 * action删除
	 * @param ids
	 * @param response
	 */
	@RequestMapping(value = "/step/delete", method = RequestMethod.POST)
	public void stepDelete(Integer[] ids, HttpServletResponse response) {
		if (ids.length == 0) {
			ResponseUtils.responseFailure(response);
		}
		try {
			List<Integer> list = Arrays.asList(ids);
			douyingActionStepConfigService.delete(list);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			ResponseUtils.responseFailure(response);
		}
	}
}
